Ext.define('BasicInfo', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
	    , "Name"   //姓名
		, "Gender"   //性别
		, "Birthday"   //生日
		, "Age"   //年龄
		, "Nationality"   //名族
		, "Marriage"   //婚姻
		, "CitizenShip"   //国籍
		, "BloodType"   //血型
		, "Birthplace"   //出生地
		, "PoliticsStatus"   //政治面貌
		, "Credential"   //证件
		, "FilesPlace"   //档案所在地
		, "DomicilePlace"   //户口所在地
		, "Photo"   //照片
		, "Email"   //邮箱
		, "PrivateEmail"   //私人邮箱
		, "MobilePhone"   //移动电话
		, "PersonalUrl"   //个人主页
		, "Interests"   //兴趣爱好
		, "Fax"   //传真
		, "Address"   //住址
        , "CustomerId" //客户Id
        , "CustomerName" //客户名称
    ],
    idProperty: "Id"
});

Ext.define('SHApp.EmployeeInfo.BasicInfo.Grid', {
    extend: 'BLBTSystem.grid',
    alias: 'shapp.employeeinfo.grid',
    requires: ['SHApp.EmployeeInfo.BasicInfo.Form',
    'SHApp.EmployeeInfo.BasicInfo.CompanyComboBox'
    ],
    txtNameID: 'txtName',
    cmbCustomerID: "_cmbCustomerID",
    btnDetailID: "_btnDetailID",
    dataModel: "BasicInfo",
    dataUrl: "api/basicinfo?format=json",
    tabInfo: null,
    initComponent: function () {
        var me = this;
        Ext.apply(this);
        me.btnAddID += me.id;
        me.btnEditID += me.id;
        me.btnDeleteID += me.id;
        me.btnDeleteID += me.id;
        me.txtNameID += me.id;
        me.cmbCustomerID += me.id;
        this.creatTbar();
        this.creatColumns();
        this.selectionChange();
        this.callParent(arguments);
        this.checkBtnPermission(me.pvalue);
    },
    selectionChange: function () {
        var me = this;
        me.on("selectionchange", function (model, records, opt) {
            if (me.down('#' + me.btnDetailID) != null)
                me.down('#' + me.btnDetailID).setDisabled(records.length == 0);
        });
    },
    creatColumns: function () {
        var me = this;
        me.columns = [{
            text: "Id",
            flex: 1,
            dataIndex: 'Id',
            sortable: false,
            hidden: true
        }, {
            text: "姓名",
            width: 100,
            dataIndex: 'Name',
            sortable: true
        }, {
            text: "性别",
            flex: 1,
            dataIndex: 'Gender',
            renderer: function (v) {
                if (v) {
                    return "男";
                } else {
                    return "女";
                }
            }
        }, {
            text: "生日",
            width: 100,
            dataIndex: 'Birthday',
            sortable: true,
            renderer: formatDate
        }, {
            text: "年龄",
            width: 60,
            dataIndex: 'Age',
            sortable: true
        }, {
            text: "名族",
            width: 100,
            dataIndex: 'Nationality',
            sortable: true
        }, {
            text: "婚姻",
            width: 60,
            dataIndex: 'Marriage',
            sortable: true
        }, {
            text: "国籍",
            width: 100,
            dataIndex: 'CitizenShip',
            sortable: true
        }, {
            text: "血型",
            width: 60,
            dataIndex: 'BloodType',
            sortable: true
        }, {
            text: "出生地",
            width: 100,
            dataIndex: 'Birthplace',
            sortable: true
        }, {
            text: "政治面貌",
            width: 100,
            dataIndex: 'PoliticsStatus',
            sortable: true
        }, {
            text: "证件",
            width: 100,
            dataIndex: 'Credential',
            sortable: true
        }, {
            text: "档案所在地",
            width: 100,
            dataIndex: 'FilesPlace',
            sortable: true
        }, {
            text: "户口所在地",
            width: 100,
            dataIndex: 'DomicilePlace',
            sortable: true
        }, {
            text: "照片",
            width: 100,
            dataIndex: 'Photo',
            sortable: true
        }, {
            text: "邮箱",
            width: 100,
            dataIndex: 'Email',
            sortable: true
        }, {
            text: "私人邮箱",
            width: 100,
            dataIndex: 'PrivateEmail',
            sortable: true
        }, {
            text: "移动电话",
            width: 100,
            dataIndex: 'MobilePhone',
            sortable: true
        }, {
            text: "个人主页",
            width: 100,
            dataIndex: 'PersonalUrl',
            sortable: true
        }, {
            text: "兴趣爱好",
            width: 100,
            dataIndex: 'Interests',
            sortable: true
        }, {
            text: "传真",
            width: 100,
            dataIndex: 'Fax',
            sortable: true
        }, {
            text: "住址",
            width: 100,
            dataIndex: 'Address',
            sortable: true
        }
		]
    },
    creatTbar: function () {
        var me = this;
        me.tbar = [
           {
               xtype: "companyComboBox",
               fieldLabel: "公司名称",
               name: "CustomerName",
               labelWidth: 60,
               width: 260,
               id: me.cmbCustomerID
           }, {
               xtype: 'textfield',
               disabled: false,
               fieldLabel: '员工姓名:',
               labelWidth: 60,
               name: 'Name',
               id: me.txtNameID,
               width: 180
           }, ' ', {
               xtype: 'button',
               text: '查询',
               iconCls: 'icon-btn-search',
               handler: function () {
                   me.clearFilters();
                   var vCustomerName = me.down("#" + me.cmbCustomerID).getDisplayValue();
                   if (vCustomerName && vCustomerName.length > 0) {
                       me.addFilter("CustomerName", vCustomerName);
                   }
                   var vName = me.down("#" + me.txtNameID).getValue();
                   if (vName && vName.length > 0) {
                       me.addFilter("Name", vName);
                   }
                   me.loadByFilter();
               }
           }, '->', {
               xtype: 'button',
               text: '添加',
               id: me.btnAddID,
               iconCls: 'icon-btn-add',
               handler: function () {
                   var win = me.getForm('SHApp.EmployeeInfo.BasicInfo.Form');
                   var customerId = me.down("#" + me.cmbCustomerID).getValue();
                   var customerName = me.down("#" + me.cmbCustomerID).getDisplayValue();
                   if (customerId == null || customerName == null) {
                       Ext.Msg.alert("提示信息", "请选择公司名称！");
                       return;
                   }
                   win.loadRecord(new BasicInfo({
                       Id: null,
                       CustomerId: customerId,
                       CustomerName: customerName,
                       Gender: true,
                       BloodType: "O",
                       PoliticsStatus: "群众",
                       Credential: "身份证",
                       Marriage: "未婚"
                   }));
                   win.Show("添加");
               }
           }, ' ', {
               xtype: 'button',
               disabled: true,
               id: me.btnEditID,
               text: '修改',
               iconCls: 'icon-btn-edit',
               handler: function () {
                   var rs = me.getSelectionModel().getSelection();
                   if (rs) {
                       if (rs.length > 0) {
                           rs[0].data.Birthday = formatDate(rs[0].data.Birthday);
                           me.EditData(me.getForm('SHApp.EmployeeInfo.BasicInfo.Form'));
                       }
                   }
               }
           }, ' ', {
               xtype: 'button',
               disabled: true,
               id: me.btnDeleteID,
               text: '删除',
               iconCls: 'icon-btn-delete',
               handler: function () {
                   me.DeleteData();
               }
           }, ' ', {
               xtype: 'button',
               id: me.btnDetailID,
               disabled: true,
               text: '详情',
               iconCls: 'icon-grid',
               handler: function () {
                   var rs = me.getSelectionModel().getSelection();
                   if (rs) {
                       if (rs.length > 0) {
                           me.Show("员工信息详情", rs[0].data.Id, rs[0].data.Name, rs[0].data.CustomerId, rs[0].data.CustomerName);
                       } else {
                           Ext.Msg.alert("提示信息", "请选择一条记录！");
                           return;
                       }
                   }
               }
           }
        ]
    },
    Show: function (title, empId, empName, customerId, customerName) {
        var me = this;
        var win = Ext.create("Ext.window.Window", {
            width: 1024,
            height: 600,
            // closeAction: "hide",
            iconCls: 'icon-grid',
            modal: true,
            resizable: false,
            // draggable: false,
            autoScroll: true,
            layout: { type: "border" },
            items: [
                 me.createTree(empId, empName, customerId, customerName),
                 me.createPanel()
                ]
        });       
        me.Win = win;
        me.Win.setTitle(title);
        me.Win.show();
    },
    createTree: function (vempId, vempName, vcustomerId, vcustomerName) {
        var me = this;
        var treePanel = Ext.create('Ext.tree.Panel', {
            title: '信息分类',
            width: 120,
            height: "100%",
            autoSync: false,
            fields: ["id", "text", "url"],
            margins: '0 0 5 0',
            root: {
                text: "信息分类",
                children: [
                        { id: 1, text: "证件信息", url: "SHApp.EmployeeInfo.Identification.Grid", leaf: true, iconCls: "icon-changeShop" },
                        { id: 2, text: "家庭信息", url: "SHApp.EmployeeInfo.FamilyInfo.Grid", leaf: true, iconCls: "icon-itemManage" },
                        { id: 3, text: "地址信息", url: "SHApp.EmployeeInfo.AddressInfo.Grid", leaf: true, iconCls: "icon-trainManage" },
                        { id: 4, text: "教育背景", url: "SHApp.EmployeeInfo.EducationInfo.Grid", leaf: true, iconCls: "icon-checkManage" },
                        { id: 5, text: "工作经验", url: "SHApp.EmployeeInfo.WorkExperience.Grid", leaf: true, iconCls: "icon-promoteManage" },
                        { id: 6, text: "在职信息", url: "SHApp.EmployeeInfo.InServiceInfo.Grid", leaf: true, iconCls: "icon-memberFile" }
                   ]
            },
            rootVisible: false,
            region: 'west',
            listeners: {
                itemclick: function (view, record, item, index, e, eOpts) {
                    if (!me.tabInfo.getComponent(record.data.id)) {
                        me.tabInfo.add(Ext.create(record.data.url, { id: record.data.id, title: record.data.text, closable: true, pvalue: me.pvalue, empId: vempId, empName: vempName, customerId: vcustomerId, customerName: vcustomerName }));
                    }
                    me.tabInfo.setActiveTab(record.data.id);
                }
            }
        });
        return treePanel;
    },
    createPanel: function () {
        var me = this;
        me.tabInfo = Ext.widget('tabpanel', {
            region: 'center',
            margins: '0 0 5 0',
            // padding: '0 5 0 0',
            layout: 'fit',
            height: "100%"
        });
        return me.tabInfo;
    }
});

